extends KinematicBody2D

export var max_speed = 500.0
var _velocity = Vector2.ZERO
var target_global_position: Vector2 = Vector2.ZERO


const MIN_DISTANCE = 3.0 # 最小距离
const SLOW_RADIUS = 200.0 # 减速范围圆的半径


onready var sprite = $player


func _ready():
	set_physics_process(false)


func _unhandled_input(event):
	if event.is_action_pressed("click"):
		target_global_position = get_global_mouse_position()
		set_physics_process(true)


func _physics_process(delta):
	
	# 角色与目标位置之间的距离小于规定的最小距离
	if global_position.distance_to(target_global_position) < MIN_DISTANCE:
		return
	
	_velocity = Steering.arrive_to(
		_velocity,
		global_position,
		target_global_position,
		max_speed,
		SLOW_RADIUS
	)
	
	_velocity = move_and_slide(_velocity)
	# 角色转向
	sprite.rotation = _velocity.angle()
